package datatypes.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import static datatypes.list.PositionOfDeletedItem.*;

public class LinkedList<E> implements List<E> {
	private List<E> list = new ArrayList<E>();
	private int lastAccessedIndex = 0;
	
	public void moveToHead() {
		lastAccessedIndex = 0;
	}
	
	public E peek() {
		if (lastAccessedIndex < list.size() || lastAccessedIndex < 0) {
			return null;
		}
		return list.get(lastAccessedIndex);
	}
	
	public boolean hasNext() {
		return lastAccessedIndex < list.size();
	}
	
	public E next() {
		if ( ! hasNext()) {
			return null;
		}
		E element = list.get(lastAccessedIndex);
		lastAccessedIndex++;
		return element;
	}
	
	public void advance() {
		lastAccessedIndex++;
	}
	
	public boolean hasPrevious() {
		return lastAccessedIndex > 0;
	}
	
	public E previous() {
		if ( ! hasPrevious() ) {
			return null;
		}
		lastAccessedIndex--;
		return list.get(lastAccessedIndex);
	}
	
	@Override
	public boolean add(E item) {
		return list.add(item);
	}

	@Override
	public void add(int index, E item) {
		list.add(index, item);
	}

	@Override
	public boolean addAll(Collection<? extends E> items) {
		return list.addAll(items);
	}

	@Override
	public boolean addAll(int index, Collection<? extends E> items) {
		return list.addAll(index, items);
	}

	@Override
	public void clear() {
		list.clear();
	}

	@Override
	public boolean contains(Object item) {
		return list.contains(item);
	}

	@Override
	public boolean containsAll(Collection<?> items) {
		return list.containsAll(items);
	}

	@Override
	public E get(int index) {
		return list.get(index);
	}

	@Override
	public int indexOf(Object item) {
		return list.indexOf(item);
	}

	@Override
	public boolean isEmpty() {
		return list.isEmpty();
	}

	@Override
	public Iterator<E> iterator() {
		return list.iterator();
	}

	@Override
	public int lastIndexOf(Object item) {
		return list.lastIndexOf(item);
	}

	@Override
	public ListIterator<E> listIterator() {
		return list.listIterator();
	}

	@Override
	public ListIterator<E> listIterator(int index) {
		return list.listIterator(index);
	}

	@Override
	public boolean remove(Object item) {
		int index = list.indexOf(item);
		if (index < 0) {
			return false;
		}
		remove(index);
		return true;
	}
	
	public PositionOfDeletedItem removeAndReportPosition(Object item) {
		int index = list.indexOf(item);
		PositionOfDeletedItem position = WAS_OTHER_POSITION;
		if (index < 0) {
			return WAS_NOT_IN_LIST;
		}
		if (index == lastAccessedIndex) {
			position = WAS_CURRENT_POSITION;
		}
		if (index == list.size()-1) {
			position = WAS_LAST_POSITION;
		}
		remove(index);
		return position;
	}

	@Override
	public E remove(int index) {
		if (index < lastAccessedIndex) {
			lastAccessedIndex--;
		}
		return list.remove(index);
	}

	@Override
	public boolean removeAll(Collection<?> items) {
		throw new UnsupportedOperationException();
//		return list.removeAll(items);
	}

	@Override
	public boolean retainAll(Collection<?> items) {
		throw new UnsupportedOperationException();
//		return list.retainAll(items);
	}

	@Override
	public E set(int index, E item) {
		throw new UnsupportedOperationException();
//		return list.set(index, item);
	}

	@Override
	public int size() {
		return list.size();
	}

	@Override
	public List<E> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray() {
		return list.toArray();
	}

	@Override
	public <T> T[] toArray(T[] array) {
		return list.toArray(array);
	}

}
